<template>
  <el-row>
    <el-col :span="24">
      <Portlet>
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">

            <el-col :lg="4" :md="6">
              <el-form-item prop="code">
                <el-input v-model="search.code" placeholder="公司编号" clearable></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="name">
                <el-input v-model="search.name" placeholder="公司名称"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="communityCode">
                <el-input v-model="search.communityCode" placeholder="统一社会代码"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="8">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="registeDate">
                <el-date-picker type="date"
                                placeholder="注册时间"
                                v-model="search.registeDate"
                                value-format="yyyy-MM-dd"
                                style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="legalPerson">
                <el-input v-model="search.legalPerson" placeholder="公司法人"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <!--<el-col :span="8">-->
                  <!--<el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>-->
                  <!--</el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="manager">
                <el-input v-model="search.manager" placeholder="总经理"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="state">
                <el-select v-model="search.state" clearable placeholder="状态">
                  <el-option label="启用" value="1"></el-option>
                  <el-option label="停用" value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="businessModeCode">
                <el-select v-model="search.businessModeCode" clearable placeholder="业务模式">
                  <el-option :key="item.value" v-for="item in businessTypes" :label="item.text" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="addressArr">
                <CascadeAddress v-model="search.addressArr" :level="3" placeholder="用户地址" changeOnSelect/>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </Portlet>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-warehouse-order/basicData/findCompanyList"
        :tabs="tabs"
        :columns="columns">
        <template slot="buttons">
          <el-button type="primary" @click="create">新增</el-button>
        </template>
        <template slot="column">
          <el-table-column fixed="right" width="100px" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="show(scope.row)"
              >
                详情
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
import {copy, dotData} from '@/utils'
import {getCompanyBusinessType} from '@/utils/selectOptions'
import {unixTimestampFilter, filterDataBaseDictionary} from '@/filters'
export default {
  name: 'basicDataCompanyIndex',
  components: {
  },
  data() {
    let columns = [
      {
        label: '公司编号',
        data: 'code'
      },
      {
        label: '账号姓名',
        data: 'userName'
      },
      {
        label: '登陆账号',
        data: 'userPhone'
      },
      {
        label: '账号岗位',
        data: 'position'
      },
      {
        label: '公司名称',
        data: 'name'
      },
      {
        label: '业务模式',
        data: 'businessModeCode',
        render: (data, type, full, meta) => {
          return filterDataBaseDictionary(data) || ''
        }
      },
      {label: '统一社会代码', data: 'communityCode'},
      {label: '公司法人', data: 'legalPerson'},
      {
        label: '注册时间',
        data: 'registeDate',
        render: (data, type, full, meta) => {
          return unixTimestampFilter(data)
        }
      },
      {label: '总经理', data: 'manager'},
      {
        label: '办公地址',
        data: 'detailAddress',
        render(data, full) {
          return `${full.province} ${full.city} ${full.district} ${data}`
        }
      },
      {
        label: '状态',
        data: 'state',
        render(data, full) {
          if (data === 1) {
            return '启用'
          } else if (data === 0) {
            return '停用'
          }
        }
      },
      {
        label: '添加时间',
        data: 'foundDate',
        width: '134px',
        render(data, full) {
          return unixTimestampFilter(data)
        }
      },
      {label: '操作人', data: 'founderName'}
    ]
    return {
      businessTypes: [],
      businessArr: [],
      serviceArr: [],
      isOpen: false,
      visible: false,
      searchLoading: false,
      search: {
        code: '',
        name: '',
        communityCode: '',
        state: '',
        registeDate: '',
        legalPerson: '',
        manager: '',
        businessModeCode: '',
        addressArr: []
      },
      columns,
      tabs: []
    }
  },
  created() {
    // 公司业务类型
    getCompanyBusinessType().then(arr => {
      this.businessTypes = arr
    })
  },
  methods: {
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        params.provinceNo = dotData(params, 'addressArr[0]') || ''
        params.cityNo = dotData(params, 'addressArr[1]') || ''
        params.districtNo = dotData(params, 'addressArr[2]') || ''
        delete params.addressArr
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    },
    show(data) {
      this.$router.push({path: '/ss-oms/basic-data/company/show/' + data.id})
    },
    create() {
      this.$router.push({path: '/ss-oms/basic-data/company/create'})
    }
  }
}
</script>
